<?php echo $header; ?>
<div id="content">
   <div class="breadcrumb">
    <?php echo $this->breadCrumbs(); ?>
  </div>
  <?php if (isset($error_warning)) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success"><?php echo $success; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/mail.png" alt="" /><?php echo $_['heading_title'];?></h1>
      <div class="buttons">
      <a onclick="location = '<?php echo UA('sale/mail_task/insert'); ?>'" class="button"><span><?php echo $_['button_insert']; ?></span></a>
      <a onclick="$('#form').submit();" class="button"><span><?php echo $_['button_delete']; ?></span></a>
      </div>
    </div>
    <div class="content">
      <form action="<?php echo UA('sale/mail_task/delete'); ?>" method="post" enctype="multipart/form-data" id="form">
        <table class="list">
          <thead>
            <tr height="30">
              <td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
			  <td>ID</td>
			  <td><?php echo $_['text_title']; ?></td>
			  <td><?php echo $_['text_mail']?></td>
			  <td><?php echo $_['text_to']?></td>
			  <td><?php echo $_['text_items']?></td>
			  <td><?php echo $_['text_sent']?></td>
			  <td><?php echo $_['text_errors']?></td>
			  <td><a <?php echo sort_url('date_added');?>><?php echo $_['text_date_added'];?></a></td>
			  <td width="100" class="right"><?php echo $_['text_action']; ?></td>
            </tr>
          </thead>
          <tbody>
            <?php if ($tasks) { ?>
            <?php foreach ($tasks as $task) { ?>
            <tr  height="30" onmouseover="this.className='on';" onmouseout="this.className='';">
              <td style="text-align: center;">
              <input type="checkbox" name="selected[]" value="<?php echo $task['mail_task_id']; ?>" /></td>
              <td><?php echo $task['mail_task_id']; ?></td>
			  <td><?php echo $task['title']; ?></td>
			  <td><?php echo $task['subject']; ?></td>
			  <td><?php echo isset($_['mail_to'][$task['to']]) ? $_['mail_to'][$task['to']] : ''; ?></td>
			  <td><?php echo $task['items']?></td>
			  <td><?php echo $task['sent']?></td>
			  <td><?php echo $task['errors']?></td>
			  <td><?php echo v5_date_format($task['date_added']); ?></td>
			  <td class="right">
			  	<?php if ($task['status'] > 0) {?>
			  	[<a href="<?php echo UA('sale/mail_task/items', 'filter_reset=1&filter_mail_task_id=' . $task['mail_task_id'])?>"><?php echo $_['text_view']?></a>]
			  	<?php } if ($task['status'] < 2) {?>
				[<a href="javascript:;" onclick="send(<?php echo $task['mail_task_id']?>)"><?php echo $_['text_send']?></a>]
				<?php } ?>
			  </td>
            </tr>
            <?php } ?>
            <?php } else { ?>
            <tr>
              <td class="center" colspan="15"><?php echo $_['text_no_results']; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </form>
	  <div class="pagination"><?php echo $pagination; ?></div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
function filter() {
	url = '<?php echo UA('sale/mail_task', 'filter_reset=1'); ?>';

	v = $('input[name=\'filter_title\']').attr('value');
	if (v != '*') {
		url += '&filter_title=' + encodeURIComponent(v);
	}
	location = url;
}

function send(mail_task_id, msg) {
	msg = msg ? msg : '';
	$.jBox.tip(msg, 'loading');
	$.getJSON('<?php echo UA('sale/mail_task/send')?>', {mail_task_id:mail_task_id}, function(rs) {
		if (rs['error']) {
			$.jBox.closeTip();
			$.jBox.error(rs['error'], '<?php echo $_['heading_title']?>');
		}
		else {
			if (rs['finished'] == 1) {
				$.jBox.closeTip();
				$.jBox.success(rs['message'], '<?php echo $_['heading_title']?>', { closed: function () { window.location.reload(); } });
			}
			else {
				$.jBox.tip(rs['message'], 'loadding');
				window.setTimeout(function() { send(mail_task_id, rs['message']); }, 2000);
			}
		}
	});
}
//--></script>
<?php echo $footer; ?>